package Calculate

import (
	"fmt"
	"gorm.io/gorm"
	"main.go/model"
	"time"
)

func GetTimeArr(start, end string) int {
	timeLayout := "2006-1-2"
	loc, _ := time.LoadLocation("Local")
	// 转成时间戳
	startUnix, _ := time.ParseInLocation(timeLayout, start, loc)
	endUnix, _ := time.ParseInLocation(timeLayout, end, loc)
	startTime := startUnix.Unix()
	endTime := endUnix.Unix()
	// 求相差天数
	date := (endTime - startTime) / 86400
	return int(date)
}
func Get_data(in_house model.In_house, out_house model.Out_house) int {
	return GetTimeArr(in_house.In_house_time, out_house.Out_house_time)
}

//func Get_tun(in_house model.In_house, out_house model.Out_house) int {
//	return in_house.In_house_tun - out_house.Out_house_tun
//}

func Calculate(db *gorm.DB, out_house model.Out_house) int {
	var house model.House
	var in_house model.In_house
	db.Where("house_id = ?", out_house.Out_house_house_id).First(&house)
	db.Where("in_house_id = ?", out_house.Out_house_id).First(&in_house)
	data := Get_data(in_house, out_house)
	tun := out_house.Out_house_tun

	if data <= 14 {
		return 0
	}
	rate := house.House_rate
	result := rate * data * tun
	fmt.Println(result)
	fmt.Println(rate)
	fmt.Println(data)
	fmt.Println(tun)
	return result
}
